package com.huaboot.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaboot.system.entity.SysUserRoleEntity;
import com.huaboot.system.mapper.SysUserRoleMapper;
import com.huaboot.system.service.SysUserRoleService;
import com.huaboot.system.service.SysUserTokenService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * description: SysUserRoleServiceImpl
 * author: ff
 */
@Service
@AllArgsConstructor
public class SysUserRoleServiceImpl  extends ServiceImpl<SysUserRoleMapper, SysUserRoleEntity> implements SysUserRoleService {

    private final SysUserTokenService sysUserTokenService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(Long userId, List<Long> roleIdList) {
        if (userId == null  ||  CollectionUtil.isEmpty(roleIdList)){
            return;
        }
        baseMapper.delete(Wrappers.<SysUserRoleEntity>lambdaQuery().eq(SysUserRoleEntity::getUserId, userId));
        List<SysUserRoleEntity> collect = roleIdList.stream().map(item -> {
            SysUserRoleEntity sysUserRole = new SysUserRoleEntity();
            sysUserRole.setRoleId(item);
            sysUserRole.setUserId(userId);
            return sysUserRole;
        }).collect(Collectors.toList());
        saveBatch(collect);
    }

    @Override
    public void saveUserList(Long roleId, List<Long> userIdList) {
        //todo 需要优化有重复用户问题
         List<SysUserRoleEntity> list = userIdList.stream().map(userId -> {
            SysUserRoleEntity entity = new SysUserRoleEntity();
            entity.setUserId(userId);
            entity.setRoleId(roleId);
            return entity;
        }).collect(Collectors.toList());

        // 批量新增
        saveBatch(list);

        // 更新用户的缓存权限
        userIdList.forEach(sysUserTokenService::updateCacheAuthByUserId);
    }

    @Override
    public void deleteByRoleIdList(List<Long> roleIdList) {
        remove(new LambdaQueryWrapper<SysUserRoleEntity>().in(SysUserRoleEntity::getRoleId, roleIdList));

    }

    @Override
    public void deleteByUserIdList(List<Long> userIdList) {
        baseMapper.delete(new LambdaQueryWrapper<SysUserRoleEntity>().in(SysUserRoleEntity::getUserId, userIdList));
    }

    @Override
    public void deleteByUserIdList(Long roleId, List<Long> userIdList) {
        LambdaQueryWrapper<SysUserRoleEntity> queryWrapper = new LambdaQueryWrapper<>();
        remove(queryWrapper.eq(SysUserRoleEntity::getRoleId, roleId).in(SysUserRoleEntity::getUserId, userIdList));

        // 更新用户的缓存权限
        userIdList.forEach(sysUserTokenService::updateCacheAuthByUserId);
    }

    @Override
    public List<Long> getRoleIdList(Long userId) {
        return baseMapper.getRoleIdList(userId);
    }
}
